package com.bdqn.gulimall.product.app;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.utils.PageUtils;
import com.bdqn.common.utils.R;
import com.bdqn.gulimall.product.entity.AttrEntity;
import com.bdqn.gulimall.product.entity.AttrGroupEntity;
import com.bdqn.gulimall.product.service.AttrAttrgroupRelationService;
import com.bdqn.gulimall.product.service.AttrGroupService;
import com.bdqn.gulimall.product.service.AttrService;
import com.bdqn.gulimall.product.service.CategoryService;
import com.bdqn.gulimall.product.vo.AttrGroupRelationVo;
import com.bdqn.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author cxk&xp&fg
 * @email 200224242@qq.com
 * @date 2022-01-22 14:51:09
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService relationService;

    /**
     * 获取分类下所有分组&关联属性
     * @return
     */
    // @RequestMapping("/{catelogId}/withattr")
    // public R catelogIdWithattr(){
    //     return null;
    // }

    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){ // catelogId:三级分类的id

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        System.out.println("withattr"+JSON.toJSONString(vos));
        return R.ok().put("data",vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        System.out.println("list==>"+JSON.toJSONString(params));
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     *  获取属性分组没有关联的其他属性
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrgroupIdNoattrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                                       @RequestParam Map<String, Object> params){
        PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     *  删除属性与分组的关联关系
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     *  添加属性与分组关联关系
     * @return
     */
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 根据分组ID查找关联的所有基本属性
     * @param attrgroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrgroupIdAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",entities);
    }

//    /**
//     * 获取分类属性分组
//     * @param page 当前页码
//     * @param limit 每页记录数
//     * @param sidx 排序字段
//     * @param order 排序方式
//     * @param key 检索关键字
//     * @return
//     */
//    @RequestMapping("/list/{catelogId}")
//    public R listCatelogId(@RequestParam Map<String, Object> params,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,@RequestParam("key") String key){
//        System.out.println("params===>"+JSON.toJSONString(params));
//        return null;
//    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        //找到当前节点的完整路劲
        Long[] path=categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
